/**
 * 
 */
package com.hw.hcit.feature;

import java.util.Random;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


/**
 * @author gaoshengpan
 *
 */
public class SpeakerTestItem extends JTestItem {
    private TextView textView,textView2,textView3;
    private MediaPlayer mMediaPlayer;
    private AudioManager mAudioManager;
    private boolean tested = false;
    private boolean powerClicked = false;
    private String TAG = "SpeakerTestItem";
    private Button mButton1,mButton2;
    private static int sourceId;
    private static int chooseTime = 0;
    private RadioButton bt1,bt2,bt3;
    private RadioGroup group;
    private Handler mHandler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            tested = true;
        }
    };

    /**
     * @param cb
     */
    public SpeakerTestItem(JCtrlCallback cb) {
        super(cb);
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     */
    public SpeakerTestItem() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mAudioManager = (AudioManager) Config.GetActivity()
                .getApplicationContext().getSystemService("audio");
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.speaker, container,
                false);
        textView = (TextView) rootView.findViewById(R.id.textView1);
        textView2 = (TextView) rootView.findViewById(R.id.test_title);
        textView3 = (TextView) rootView.findViewById(R.id.textView2);
        textView2.setText(getString(R.string.speaker_test));
        textView.setText(getString(R.string.speaker_test));
        bt1 = (RadioButton)rootView.findViewById(R.id.radio0);
        bt2 = (RadioButton)rootView.findViewById(R.id.radio1);
        bt3 = (RadioButton)rootView.findViewById(R.id.radio2);
        group = (RadioGroup)rootView.findViewById(R.id.radioGroup1);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				// TODO Auto-generated method stub
				chooseTime++;
				switch(checkedId){
				case R.id.radio2:
					if(chooseTime<=1){
					if(sourceId == R.raw.test1 ){
						 mButton1.setEnabled(true);
						 textView3.setText(getString(R.string.right_choice));
					}else{
						textView3.setText(getString(R.string.wrong_choice));
					}
					}
					textView3.setVisibility(View.VISIBLE);
					break;
				case R.id.radio0:
					if(chooseTime<=1){
					if(sourceId == R.raw.testl ){
						 mButton1.setEnabled(true);
						 textView3.setText(getString(R.string.right_choice));
					}else{
						textView3.setText(getString(R.string.wrong_choice));
					}
					}
					textView3.setVisibility(View.VISIBLE);
					break;
				case R.id.radio1:
					if(chooseTime<=1){
					if(sourceId == R.raw.testr ){
						 mButton1.setEnabled(true);
						 textView3.setText(getString(R.string.right_choice));
					}else{
						textView3.setText(getString(R.string.wrong_choice));
					}
					}
					textView3.setVisibility(View.VISIBLE);
					break;
				}
			}
		});
        mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(14, 'P');
					} else {
						rewriteOneByteOfNv2499(45, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(14, 'F');
					} else {
						rewriteOneByteOfNv2499(45, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
        return rootView;
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        tested = false;
        powerClicked = false;
        chooseTime = 0;
        textView3.setVisibility(View.GONE);
        mButton1.setEnabled(false);
        bt1.setChecked(false);
        bt2.setChecked(false);
        bt3.setChecked(false);
        if (mMediaPlayer == null) {
            Log.d(TAG, "mMediaPlayer---------->Null");
            Random random = new Random(); 
            int i =(int) random.nextInt(3);
            Log.d(TAG, "id-------->"+"="+i+"\n"+random.nextInt(3));
			switch (i) {
			case 0:
				sourceId = R.raw.test1;
				break;
			case 1:
				sourceId = R.raw.testl;
				break;
			case 2:
				sourceId = R.raw.testr;
				break;

			default:
				break;
			}
            mMediaPlayer = MediaPlayer.create(Config.GetActivity()
                    .getApplicationContext(), sourceId);
            if (mMediaPlayer != null) {
                Log.d(TAG, "mMediaPlayer---------->notNull");
            }
            try {
                mMediaPlayer.setVolume(1f,1f);
                mMediaPlayer.setLooping(true);
                mMediaPlayer.start();
                mHandler.sendEmptyMessageDelayed(0, 1500);
            } catch (IllegalStateException e) {
                // TODO: handle exception
                e.printStackTrace();

                Log.d(TAG, "mMediaPlayer---------->notNul" + e);
            }

        }
    }
    
    
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        Log.d(TAG, "mMediaPlayer---------->onpause");
        bt1.setChecked(false);
        bt2.setChecked(false);
        bt3.setChecked(false);
        super.onPause();
    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        Log.d(TAG, "mMediaPlayer---------->stop");
        super.onStop();
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        
        super.onDestroy();
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        return true;
    }
   /* @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
            dissmissPowerClicked();
            Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
            if (powerClicked) {
                Log.d(TAG,
                        "powerClicked --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(false, 1, false);
                return true;
            }
            if (tested) {
                Log.d(TAG,
                        "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(true, 1, true);
            } else {
                Log.d(TAG, " --------->showTestUncomplete");
                showTestUncomplete();
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
            dissmissPowerClicked();
            testFinish(true, -1, true);
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
            Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_POWER");
            powerClicked = true;
            showPowerClicked();
        }
        return true;
    }*/

    

}
